﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using QuanLyDoiXe.lop_doi_tuong;
using QuanLyDoiXe.Lop_du_lieu;
using System.IO;
using BarcodeLib;

namespace QuanLyDoiXe
{
    public partial class frmPhancong : Form
    {
        SqlConnection conn = new SqlConnection("Data Source=ACER;Initial Catalog=quanlydoixevinamilk;Integrated Security=True");
        public frmPhancong()
        {
            InitializeComponent();
        }
        #region 1. Xữ lý 
        private void btnthoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnnhaplai_Click(object sender, EventArgs e)
        {
            txtnhanvien.Text = "";
            txttaixe.Text = "";
            txtxe.Text = "";
            txtbatdau.Text = "";
            txtketthuc.Text = "";
            txtngayphancong.Text = "";
        }
        #endregion
        #region 2. Hien thi phan cong
        ListViewItem item;
        private void HienThiPhanCong()
        {
            try
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand("Select * from phancon,NVphancong,taixe,xecongty where phancon.NNVPC=NVphancong.MNVPC and phancon.Mtaixe=taixe.Mtaixe and phancon.Mxe=xecongty.Mxe ", conn);
                SqlDataReader sqlDA = cmd.ExecuteReader();
                int i = 0;
                lvphancong.View = View.Details;
                lvphancong.FullRowSelect = true;
                while (sqlDA.Read())
                {
                    i++;
                    item = new ListViewItem(i.ToString());
                    item.SubItems.Add(sqlDA["Mxe"].ToString());
                    item.SubItems.Add(sqlDA["Tentaixe"].ToString());
                    item.SubItems.Add(sqlDA["Thoigianbatdau"].ToString());
                    item.SubItems.Add(sqlDA["Thoigianketthuc"].ToString());
                    item.SubItems.Add(sqlDA["NgayPC"].ToString());
                    item.SubItems.Add(sqlDA["TenNV"].ToString());
                    lvphancong.Items.Add(item);
                }
                cmd.Dispose();
                sqlDA.Close();
                conn.Close();
            }
            catch
            {
            }
        }
        string Mxe, TenNV, Tentaixe;
        DateTime NgayPC;
        DateTime Thoigianbatdau;
        DateTime Thoigianketthuc;
        private void lvphancong_SelectedIndexChanged(object sender, EventArgs e)
        {
            for (int i = 0; i < lvphancong.Items.Count; i++)
            {
                if (lvphancong.Items[i].Selected)
                {
                    Mxe = item.SubItems[1].Text;
                    Tentaixe = item.SubItems[2].Text;
                    TenNV = item.SubItems[6].Text;
                    NgayPC = Convert.ToDateTime(item.SubItems[5].Text);
                    Thoigianbatdau = Convert.ToDateTime(item.SubItems[3].Text);
                    Thoigianketthuc = Convert.ToDateTime(item.SubItems[4].Text);
                }
            }
        }
        private void lvphancong_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
        {
            ListViewItem item = e.Item;
            txtxe.Text = item.SubItems[1].Text;
            txttaixe.Text = item.SubItems[2].Text;
            txtbatdau.Text = item.SubItems[3].Text;
            txtketthuc.Text = item.SubItems[4].Text;
            txtngayphancong.Text = item.SubItems[5].Text;
            txtnhanvien.Text = item.SubItems[6].Text;
        }
        private void frmPhancong_Load(object sender, EventArgs e)
        {
            HienThiPhanCong();
        }
        #endregion
        #region 3. Xoa
        private void btnxoa_Click(object sender, EventArgs e)
        {
            lvphancong.Items.Remove(lvphancong.FocusedItem);
            Lop_lay_du_lieu dl = new Lop_lay_du_lieu();
            SqlDataReader dr = dl.lay_reader("select NgayPC from phancon where Mxe='" + txtxe.Text + "'");
            while (dr.Read())
            {
                NgayPC = Convert.ToDateTime(dr["NgayPC"].ToString());
            }
            lop_ket_noi.huyketnoi();
            Lop_lay_du_lieu dl1 = new Lop_lay_du_lieu();
            SqlDataReader dr1 = dl1.lay_reader("select Thoigianbatdau from Phancon where Mxe='" + txtxe.Text + "'");
            while (dr1.Read())
            {
                Thoigianbatdau = Convert.ToDateTime(dr1["Thoigianbatdau"].ToString());
            }
            lop_ket_noi.huyketnoi();
            Lop_lay_du_lieu dl2 = new Lop_lay_du_lieu();
            SqlDataReader dr2 = dl2.lay_reader("select Thoigianketthuc from Phancon where Mxe='" + txtxe.Text + "'");
            while (dr2.Read())
            {
                Thoigianbatdau = Convert.ToDateTime(dr2["Thoigianketthuc"].ToString());
            }
            lop_ket_noi.huyketnoi();


            string strMaxe = "";
            Lop_lay_du_lieu dl3 = new Lop_lay_du_lieu();
            SqlDataReader dr3 = dl3.lay_reader("select Mxe from xecongty where Mxe=N\'" + txtxe.Text + "\'");
            while (dr3.Read())
            {
                strMaxe = dr3.GetString(0);
            }
            dr3.Close(); dr3.Dispose();
            string strMaNV = "";
            Lop_lay_du_lieu dl4 = new Lop_lay_du_lieu();
            SqlDataReader dr4 = dl4.lay_reader("select MNVPC from NVphancong where TenNV=N\'" + txtnhanvien.Text + "\'");
            while (dr4.Read())
            {
                strMaNV = dr4.GetString(0);
            }
            dr4.Close(); dr4.Dispose();

            string strtaixe = "";
            Lop_lay_du_lieu dl5 = new Lop_lay_du_lieu();
            SqlDataReader dr5 = dl5.lay_reader("select Mtaixe from taixe where Tentaixe=N\'" + txttaixe.Text + "\'");
            while (dr5.Read())
            {
                strtaixe = dr5.GetString(0);
            }
            dr5.Close(); dr5.Dispose();
            lop_phancong phancong = new lop_phancong(strMaxe,strMaNV,strtaixe,NgayPC,Thoigianbatdau,Thoigianketthuc);
            if (phancong.xoa())
            {
                MessageBox.Show("Quá trình xóa phân cong tai xe ra khỏi danh sách thành công !");
                txtxe.Text = "";
                txtnhanvien.Text = "";
                txttaixe.Text = "";
                txtngayphancong.Text = "";
                txtbatdau.Text = "";
                txtketthuc.Text = "";
            }

            else
                MessageBox.Show("Quá trinh xóa phân công ra khỏi danh thất bại !");
            lvphancong.Items.Clear();
            HienThiPhanCong();
        }
        #endregion
        #region 4. cập nhập
        private void btncapnhap_Click(object sender, EventArgs e)
        {
            string strMaxe = "";
            Lop_lay_du_lieu dl = new Lop_lay_du_lieu();
            SqlDataReader dr = dl.lay_reader("select Mxe from xecongty where Mxe=N\'" + txtxe.Text + "\'");
            while (dr.Read())
            {
                strMaxe = dr.GetString(0);
            }
            dr.Close(); dr.Dispose();
            string strMaNV = "";
            Lop_lay_du_lieu dl1 = new Lop_lay_du_lieu();
            SqlDataReader dr1 = dl1.lay_reader("select MNVPC from NVphancong where TenNV=N\'" + txtnhanvien.Text + "\'");
            while (dr1.Read())
            {
                strMaNV = dr1.GetString(0);
            }
            dr1.Close(); dr1.Dispose();

            string strtaixe = "";
            Lop_lay_du_lieu dl2 = new Lop_lay_du_lieu();
            SqlDataReader dr2 = dl2.lay_reader("select Mtaixe from taixe where Tentaixe=N\'" + txttaixe.Text + "\'");
            while (dr2.Read())
            {
                strtaixe = dr2.GetString(0);
            }
            dr2.Close(); dr2.Dispose();
            lop_ket_noi.huyketnoi();
            if (txtxe.Text != "" && txtnhanvien.Text != "" && txttaixe.Text != "" && txtngayphancong.Text != "" && txtbatdau.Text != "" && txtketthuc.Text != "")
            {
                lop_phancong phancong = new lop_phancong(strMaxe,strMaNV,strtaixe,Convert.ToDateTime(txtngayphancong.Text),Convert.ToDateTime(txtbatdau.Text),Convert.ToDateTime(txtketthuc.Text));
                if (phancong.capnhat())
                    MessageBox.Show("Quá trình cập nhật phân công thành công!");
                else
                    MessageBox.Show("Quá trình cập nhật phân công thất bại!");
            }
            else
                MessageBox.Show("Bạn hãy nhập đầy đủ thông tin xe! ");
            lvphancong.Items.Clear();
            HienThiPhanCong();
        }
        #endregion
        #region 5. phân công
        private void btnthem_Click(object sender, EventArgs e)
        {
            string strMaxe = "";
            Lop_lay_du_lieu dl = new Lop_lay_du_lieu();
            SqlDataReader dr = dl.lay_reader("select Mxe from xecongty where Mxe=N\'" + txtxe.Text + "\'");
            while (dr.Read())
            {
                strMaxe = dr.GetString(0);
            }
            dr.Close(); dr.Dispose();
            string strMaNV = "";
            Lop_lay_du_lieu dl1 = new Lop_lay_du_lieu();
            SqlDataReader dr1 = dl1.lay_reader("select MNVPC from NVphancong where TenNV=N\'" + txtnhanvien.Text + "\'");
            while (dr1.Read())
            {
                strMaNV = dr1.GetString(0);
            }
            dr1.Close(); dr1.Dispose();

            string strtaixe = "";
            Lop_lay_du_lieu dl2 = new Lop_lay_du_lieu();
            SqlDataReader dr2 = dl2.lay_reader("select Mtaixe from taixe where Tentaixe=N\'" + txttaixe.Text + "\'");
            while (dr2.Read())
            {
                strtaixe = dr2.GetString(0);
            }
            dr2.Close(); dr2.Dispose();
            lop_ket_noi.huyketnoi();
            txtxe.Enabled = true;
            if (txtxe.Text != "" && txtnhanvien.Text != "" && txttaixe.Text != "" && txtngayphancong.Text != "" && txtbatdau.Text != "" && txtketthuc.Text != "")
            {
                lop_phancong phancong = new lop_phancong(strMaxe, strMaNV, strtaixe, Convert.ToDateTime(txtngayphancong.Text), Convert.ToDateTime(txtbatdau.Text), Convert.ToDateTime(txtketthuc.Text));
                if (phancong.taomoi())
                    MessageBox.Show("Quá trình phân công thành công!");
                else
                    MessageBox.Show("Quá trình phân công thất bại!");
            }
            else
                MessageBox.Show("Bạn hãy nhập đầy đủ thông tin phân công! ");
            lvphancong.Items.Clear();
            HienThiPhanCong();
        }
        #endregion
    }
}
